using ApiDoc.DAL;
using ApiDoc.Models.Components;
using ApiDoc.Utility;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MySql.Data.MySqlClient;
using Oracle.ManagedDataAccess.Client;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace ApiDoc.Admin.Server
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers(options => options.ModelMetadataDetailsProviders.Add(new CustomMetadataProvider()));
             
            services.AddTransient<BaseDAL>();
            services.AddTransient<DbHelper>();
            services.AddTransient<FolderDAL>();
            services.AddTransient<ApiDAL>();
            services.AddTransient<ParamDAL>();
            services.AddTransient<FlowStepParamDAL>();
            services.AddTransient<FlowStepDAL>();
            
            string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string jsonName = basePath + "\\Configs\\MyConfig.json";
            MyConfig myConfig = new JsonFileHelper(jsonName).Read<MyConfig>();
            services.AddSingleton(myConfig);

            //Oracle
            services.AddTransient<IDbConnection, OracleConnection>();
            services.AddTransient<IDbDataAdapter, OracleDataAdapter>();
            services.AddTransient<DbParameter, OracleParameter>();

            //SqlServer
            services.AddTransient<IDbConnection, SqlConnection>();
            services.AddTransient<IDbDataAdapter, SqlDataAdapter>();
            services.AddTransient<DbParameter, SqlParameter>();

            //MySql
            services.AddTransient<IDbConnection, MySqlConnection>();
            services.AddTransient<IDbDataAdapter, MySqlDataAdapter>();
            services.AddTransient<DbParameter, MySqlParameter>();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
